/* $Header: mio.h,v 2.2 01/10/02 10:30:31 przybyls Exp $ */


/***********************************************************************/
/* INCLUDE FILE NAME: mio.h                                           */
/* ==================                                                  */
/*                                                                     */
/* Author:  Bob Cook                                                   */
/*                                                                     */
/* COMMENT:                                                            */
/*          Include file for files using MIO packets                   */
/*                                                                     */
/* NOTE: See Modular I/O Specification for the definition of MIO       */
/*       packets.                                                      */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef MIO_H
#define MIO_H

/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/


/* define buffer type for MIO buf_type field */
#define  DATA_RD         0
#define  DATA_WR         1

/* definitions for command field for MIO header */
#define  WRITE_DATA        0x00   /* packet contains write data to send */

/* definitions for command modifier field for MIO header */
#define  SEND_DATA        0x00   /* packet contains write data to send */
#define  COMPLETE_MSG     0x01   /* write packet ends with end of msg */
#define  PARTIAL_MSG      0x03   /* message continues to next write packet */

/* define return status for MIO ret_stat field */
#define  RET_OK             0x01 /* read packet contains end of msg */
#define  RET_NOT_EXECUTED   0x02 /* read request not understood and not executed */
#define  RET_ABNORMAL_BREAK 0x04 /* abnormal break in datacomm at last byte */
#define  RET_DATA_OVERRUN   0x05 /* receiver over-run occured in last message*/
#define  RET_DATA_ERR       0x06 /* data error as parity or framing error */
#define  RET_NORMAL_BREAK   0x07 /* normal break in comm. - connection down */
#define  RET_LOGICAL_BREAK  0x08 /* logical break in data - treat as RET_OK */
#define  RET_NEW_CONN       0x09 /* new logical connection with host */
#define  RET_PARTIAL_MSG    0x0B /* message continues to next read packet */
#define  RET_END_OF_JOB     0x0C /* read OK end of job - used for Apple Talk */


/*****************************/
/*   MACROS                  */
/*****************************/


/*==========================================================================*
 * MACRO: GetPacket                                                         *
 *                                                                          *
 * PURPOSE: remove packet from the head of a singly linked list called      *
 *          "queue" and sets the pointer "mio_hdr" pointing to the packet.  *
 * WARNING:                                                                 *
 *==========================================================================*/
/* GetPacket(MIO_HDR_TYPE *mio_hdr,QUEUE_TYPE  queue) */

#define GetPacket(pmio_hdr, queue)                                          \
{                                                                           \
   INTERRUPTS_OFF;                                                          \
   if((queue).num_entries == 0)                                             \
   {                                                                        \
      /* indicate no more entries available */                              \
      (pmio_hdr) = (MIO_HDR_TYPE *) NULL;                                   \
   }                                                                        \
   else                                                                     \
   {                                                                        \
      /* remove entry from queue and update count */                        \
      (pmio_hdr) = (queue).head;                                            \
      (queue).head = (pmio_hdr)->next;                                      \
      (queue).num_entries--;                                                \
   }                                                                        \
   INTERRUPTS_ON;                                                           \
}                                                                           \



/*==========================================================================*
 * MACRO: AppendPacket                                                      *
 *                                                                          *
 * PURPOSE: appends indicated packet "pmio_hdr" to the end of the specified *
 *           singly linked list called "queue".                             *
 * WARNING:                                                                 *
 *==========================================================================*/

/* AppendPacket(MIO_HDR_TYPE *pmio_hdr,QUEUE_TYPE  queue) */

#define AppendPacket(pmio_hdr, queue)                                       \
{                                                                           \
   INTERRUPTS_OFF;                                                          \
   (pmio_hdr)->next = (MIO_HDR_TYPE *)NULL;                                 \
   if ( (queue).num_entries == 0 )                                          \
   {                                                                        \
      /* set the head and tail pointers to the new entry */                 \
      (queue).head = (queue).tail = (pmio_hdr);                             \
   }                                                                        \
   else                                                                     \
   {                                                                        \
      /* set current tail's pointer to the new entry */                     \
      (queue).tail -> next = (pmio_hdr);                                    \
      (queue).tail = (pmio_hdr);                                            \
   }                                                                        \
   (queue).num_entries++;                                                   \
   INTERRUPTS_ON;                                                           \
}                                                                           \


/*****************************/
/*   TYPEDEFS                */
/*****************************/


/* Define data structures for MIO packet headers.  */
/* This data structure is fixed by the MIO standard */

typedef  struct Mio_hdr_type
{
    struct Mio_hdr_type *next;    /* pointer to next header */
    U_INT16    comm_port;         /* comm port for cmd-HPIB,DUARTA,DUARTB,MIO */
    U_INT16    dev_specific;      /* undefined for MIO - device specific */
    U_INT8     cmd;               /* command */
    U_INT8     cmd_mod;           /* command modifier */
    U_INT8     return_stat;       /* length of valid data */
    U_INT8     buf_type;          /* length of valid data */
/*---------changed by wangwei------------*/
    U_INT32    data_start;        /* address of start of data */
/*-----------end change------------------*/
    U_INT16    data_length;       /* length of data */
    U_INT16    max_data_len;      /* maximum length of buffer */
}  MIO_HDR_TYPE;


/* Define data structures for queue of MIO packet headers.  */

typedef  struct
{
    MIO_HDR_TYPE  *head;           /* pointer to head of queue */
    MIO_HDR_TYPE  *tail;           /* pointer to tail of queue */
    U_INT16        num_entries;    /* number of entries in queue */
}  QUEUE_TYPE;

#endif
